package com.example.photography.fragment;

import android.util.Log;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.photography.R;
import com.example.photography.adapter.ViewPagerAdapter;
import com.example.photography.common.BaseFragment;
import com.example.photography.fragment.home.Home_Artaicle;
import com.example.photography.fragment.home.Home_course;
import com.example.photography.fragment.home.Home_focus;
import com.example.photography.fragment.home.Home_recommend;
import com.example.photography.fragment.home.Home_vlog;
import com.example.photography.util.UIUtils;
import com.example.photography.view.CommentExpandableListView;
import com.example.photography.view.WrapContentHeightViewPager;
import com.google.android.material.tabs.TabLayout;
import com.loopj.android.http.RequestParams;
import com.shuyu.gsyvideoplayer.GSYVideoManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static androidx.constraintlayout.motion.widget.MotionScene.TAG;

/**
 * 首页
 */
public class HomeFragment extends BaseFragment {

    @BindView(R.id.tb_home)
    TabLayout tbHome;
    @BindView(R.id.vp_invest)
    ViewPager vpInvest;
    private ViewPagerAdapter viewPagerAdapter;

    @Override
    protected RequestParams getParams() {
        return null;
    }

    @Override
    protected String getUrl() {
        return null;
    }

    @Override
    protected void initData(String content) {

        initview();//初始化布局
        initListen();

    }

    private void initListen() {

    }

    private void initview() {

        //1. 加载多个不同的Fragment
        initFragments();
        //2.ViewPager设置多个Fragment的显示
        viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager(), UIUtils.getStringArr(R.array.home_tab), fragmentList);
        vpInvest.setAdapter(viewPagerAdapter);
        vpInvest.setCurrentItem(1);//设置viewpager的初始化位置
        tbHome.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
//                在这里可以设置选中状态下  tab字体显示样式
                // TODO: 2020/11/11 大小和颜色修改失败
//                vpInvest.setCurrentItem(tab.getPosition());
//                View view = tab.getCustomView();
//                if (view instanceof TextView) {
//                    ((TextView) view).setTextSize(35);
////                    ((TextView) view).setTextColor(ContextCompat.getColor(getActivity(), R.color.black));
//                }
                Log.i(TAG, "onTabSelected:" + tab.getText());
                vpInvest.setCurrentItem(tab.getPosition());
                View view = tab.getCustomView();
                if (null == view) {
                    tab.setCustomView(R.layout.tab_layout_text);
                }
                TextView textView = tab.getCustomView().findViewById(android.R.id.text1);
                textView.setTextAppearance(getActivity(), R.style.TabLayoutTextSize_select);

//                TextView textView = new TextView(getActivity());
//                float selectedSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 16, getResources().getDisplayMetrics());
//                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,selectedSize);
//                textView.setTextColor(getResources().getColor(R.color.black));
//                textView.setText(tab.getText());
//                tab.setCustomView(textView);

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.setCustomView(null);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        //将TabLayout绑定ViewPager
        tbHome.setupWithViewPager(vpInvest);

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_home;
    }

    private List<Fragment> fragmentList = new ArrayList<>();

    private void initFragments() {
        //添加到集合中
        fragmentList.add(new Home_focus());
        fragmentList.add(new Home_recommend());
        fragmentList.add(new Home_Artaicle());
        fragmentList.add(new Home_vlog());
        fragmentList.add(new Home_course());
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            //隐藏时所作的事情
            GSYVideoManager.releaseAllVideos();
        } else {
            //显示时所作的事情
            GSYVideoManager.onResume();

        }
    }
}
